package logic

import (
	"context"
	"github.com/pkg/errors"
	"mall/app/product/cmd/api/internal/svc"
	"mall/app/product/cmd/api/internal/types"
	"mall/app/product/cmd/rpc/product"
	"mall/common/xerr"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddCategoryLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddCategoryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddCategoryLogic {
	return &AddCategoryLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddCategoryLogic) AddCategory(req *types.AddCategoryReq) (resp *types.AddCategoryResp, err error) {
	category, err := l.svcCtx.ProductRpc.AddCategory(l.ctx, &product.CategoryEntity{
		Name:         req.Name,
		ParentCid:    req.ParentCid,
		CatLevel:     req.CatLevel,
		ShowStatus:   req.ShowStatus,
		Sort:         req.Sort,
		Icon:         req.Icon,
		ProductUnit:  req.ProductUnit,
		ProductCount: req.ProductCount,
	})
	if err != nil {
		return nil, errors.Wrapf(xerr.NewErrorWithMsg("新增分类失败"), "err: %v, req %+v", err, req)
	}
	return &types.AddCategoryResp{CateId: category.GetCatId()}, nil
}
